const tab = require('../models/tab');
const { add, page, list, info, update, del } = require('./utils');

let addData = async (body, res) => {
	// bulkCreate;
	await tab.bulkCreate(body);
	res.json({
		code: 0,
		data: null,
		msg: 'success'
	});
};
let getPage = (body, res) => {
	page(tab, body, res, ['name']);
};
let getList = (body, res) => {
	list(tab, body, res);
};
let getInfo = (body, res) => {
	info(tab, body, res);
};
let updateData = async (body, res) => {
    body.forEach(item => {
        item.updated_time = Date.parse(new Date()) / 1000;
    });
	await tab
		.bulkCreate(body, {
			updateOnDuplicate: ['name','updated_time']
		})
		.then(result => {
			res.json({
				code: 0,
				data: null,
				msg: 'success'
			});
		})
		.catch(err => {
			res.json({
				code: 500,
				data: null,
				msg: err
			});
		});
};
let deleteData = (body, res) => {
	del(tab, body, res);
};

module.exports = {
	addData,
	getPage,
	getList,
	getInfo,
	updateData,
	deleteData
};
